package com.dexilaonian.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.dexilaonian.MainActivity;
import com.dexilaonian.R;
import com.dexilaonian.activity.Bind_GuardActivity;
import com.dexilaonian.activity.ConsultationActivity;
import com.dexilaonian.activity.HealthAssActivity;
import com.dexilaonian.activity.HomeDetailActivity;
import com.dexilaonian.activity.LocalUtils;
import com.dexilaonian.activity.LoginActivity;
import com.dexilaonian.activity.OnlineVolunteerActivity;
import com.dexilaonian.activity.SelectCityActivity;
import com.dexilaonian.activity.Smart_guardActivity;
import com.dexilaonian.activity.UserActivity;
import com.dexilaonian.adpter.ServiceDetailAdaprer;
import com.dexilaonian.bean.HomeNewsBean;
import com.dexilaonian.permission.PermissionUtils;
import com.dexilaonian.photo.activity.VolunteerActivity;
import com.dexilaonian.ui.BaseFragment;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;
import com.dexilaonian.utils.JsonUtil;

import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.utils.PreferencesUtil;
import com.dexilaonian.view.LoadingDialog;
import com.dexilaonian.view.SlideShowView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class HomeFragment extends BaseFragment implements View.OnClickListener {
    private static final int RESULT_OK = 21;
    private static final int CAMERA_PERIMISSION_CODE = 22;
    @Bind(R.id.tv_home_weizhi)
    TextView tvHomeWeizhi;
    @Bind(R.id.home_slideshow)
    SlideShowView homeSlideshow;
    @Bind(R.id.ll_expert_consultation)
    LinearLayout llExpertConsultation;
    @Bind(R.id.ll_smart_guard)
    LinearLayout llSmartGuard;
    @Bind(R.id.ll_community_activity)
    LinearLayout llCommunityActivity;
    @Bind(R.id.ll_volunteer_team)
    LinearLayout llVolunteerTeam;
    @Bind(R.id.tv_home_new)
    TextView tvHomeNew;
    @Bind(R.id.iv_home_new)
    View ivHomeNew;
    @Bind(R.id.tv_home_chronic)
    TextView tvHomeChronic;
    @Bind(R.id.iv_home_chronic)
    View ivHomeChronic;
    @Bind(R.id.tv_home_recover)
    TextView tvHomeRecover;
    @Bind(R.id.iv_home_recover)
    View ivHomeRecover;
    @Bind(R.id.tv_home_food)
    TextView tvHomeFood;
    @Bind(R.id.iv_home_food)
    View ivHomeFood;
    @Bind(R.id.tv_home_emergency)
    TextView tvHomeEmergency;
    @Bind(R.id.iv_home_emergency)
    View ivHomeEmergency;
    @Bind(R.id.home_ViewPager_new)
    ViewPager homeViewPagerNew;
    @Bind(R.id.home_scrollView)
    ScrollView homeScrollView;

    private String auto_url = Constant.base_url + "/banner/getBanners";
    private String home_url = Constant.base_url + "/partner/list";
    private List<View> home_list = new ArrayList<>();
    private List<HomeNewsBean> list_grid = new ArrayList();
    private String MyCity = "深圳";
    private Intent intent;
    private String selectcity = "";
    private View mNewsView;
    private View mChronic;
    private View mRecovery;
    private View mFood;
    private View mEmergency;
    private ListView listviewNews;
    private ListView listviewChronic;
    private ListView listviewRecovery;
    private ListView listviewFood;
    private ListView listviewEmergency;
    private int home_id;
    private List<String> url_list = new ArrayList<>();
    private String banner;
    private FragmentActivity mContext;
    private LoadingDialog dialog;
    private String url3;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (mContext == null) {
            mContext = getActivity();
        }
        MainActivity activity = (MainActivity) mContext;
        selectcity = activity.getCity();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View layoutView) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        // Log.e("TTThome线程0", Thread.currentThread().getName());
        dialog = new LoadingDialog(mContext);
        url3 = Constant.base_url + "/user/getMyInfo.do";
        new Thread(new Runnable() {
            @Override
            public void run() {
                initAuto();
            }
        }).start();
        initHome();
        tvHomeNew.setTextSize(16);
        tvHomeChronic.setTextSize(14);
        tvHomeRecover.setTextSize(14);
        tvHomeFood.setTextSize(14);
        tvHomeEmergency.setTextSize(14);
        initNews(0);
        if (TextUtils.isEmpty(selectcity)) {
          //init();
        } else {
            tvHomeWeizhi.setText(selectcity);
        }
        homeScrollView.smoothScrollTo(0, 20); //回到顶部
        return rootView;
    }

    //定位
    private void getLocation() {
        LocalUtils localUtils = new LocalUtils(mContext, new LocalUtils.OnLocationListener() {
            @Override
            public void locationListener(final String city) {
                mContext.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (city != null) {
                            PreferencesUtil.putString(mContext, Constant.CITY, city);
                            tvHomeWeizhi.setText(city);
                            MyCity = city;
                        }
                    }
                });
            }
        });
        localUtils.startLocation();
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_home;
    }

    private void init() {
        //请求定位允许
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 12);
            getLocation();
        } else {
            getLocation();
        }
    }

    //轮播图
    private void initAuto() {
        OkHttpUtils.post().url(auto_url).build().execute(new StringCallback() {
            private String banner;

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ArrayList<String> bannerPics = new ArrayList<String>();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(mContext, LoginActivity.class));
                        return;
                    }
                    if (!jsonObject.toString().equals("")) {
                        String data1 = jsonObject.optString("data");
                        if (!TextUtils.isEmpty(data1)) {
                            JSONArray json = new JSONArray(data1);
                            bannerPics.clear();
                            for (int i = 0; i < json.length(); i++) {
                                String imagePath = json.getString(i);
                                JSONObject jsono = new JSONObject(imagePath);
                                banner = jsono.optString("banner");
                                bannerPics.add(Constant.base_pic_url + banner);
                            }
                            if (bannerPics.size() != 0 &&homeSlideshow != null) {
                                homeSlideshow.setData(bannerPics, mContext);
                                homeSlideshow.initShow();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
//加载新闻列表与点击后操作
    private void initHome() {
        mNewsView = View.inflate(mContext, R.layout.fragment_new,
                null);
        listviewNews = (ListView) mNewsView.findViewById(R.id.listview_news);

        mChronic = View.inflate(mContext,
                R.layout.fragment_chronic,
                null);
        listviewChronic = (ListView) mChronic.findViewById(R.id.listview_chronic);

        mRecovery = View.inflate(mContext,
                R.layout.fragment_reconvery,
                null);
        listviewRecovery = (ListView) mRecovery.findViewById(R.id.listview_recovery);

        mFood = View.inflate(mContext,
                R.layout.fragment_food,
                null);
        listviewFood = (ListView) mFood.findViewById(R.id.listview_food);
        mEmergency = View.inflate(mContext,
                R.layout.fragment_emergency,
                null);
        listviewEmergency = (ListView) mEmergency.findViewById(R.id.listview_emregency);
        home_list.clear();
        home_list.add(mNewsView);
        home_list.add(mChronic);
        home_list.add(mRecovery);
        home_list.add(mFood);
        home_list.add(mEmergency);

        tvHomeNew.setOnClickListener(this);
        tvHomeChronic.setOnClickListener(this);
        tvHomeRecover.setOnClickListener(this);
        tvHomeFood.setOnClickListener(this);
        tvHomeEmergency.setOnClickListener(this);
        homeViewPagerNew.setCurrentItem(0);
        homeViewPagerNew.setOffscreenPageLimit(5);
        ServiceDetailAdaprer adapter = new ServiceDetailAdaprer(mContext, home_list);
        homeViewPagerNew.setAdapter(adapter);
        homeViewPagerNew.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                chanceColorText(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void chanceColorText(int position) {
        if (position == 0) {
            homeViewPagerNew.setCurrentItem(0);
            tvHomeNew.setTextColor(Color.parseColor("#3aafff"));
            tvHomeNew.setTextSize(16);
            tvHomeChronic.setTextSize(14);
            tvHomeRecover.setTextSize(14);
            tvHomeFood.setTextSize(14);
            tvHomeEmergency.setTextSize(14);

            tvHomeChronic.setTextColor(Color.parseColor("#333333"));
            tvHomeRecover.setTextColor(Color.parseColor("#333333"));
            tvHomeFood.setTextColor(Color.parseColor("#333333"));
            tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
            ivHomeNew.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHomeChronic.setBackground(null);
            ivHomeRecover.setBackground(null);
            ivHomeFood.setBackground(null);
            ivHomeEmergency.setBackground(null);
            initNews(0);

        } else if (position == 1) {
            homeViewPagerNew.setCurrentItem(1);
            tvHomeNew.setTextSize(14);
            tvHomeChronic.setTextSize(16);
            tvHomeRecover.setTextSize(14);
            tvHomeFood.setTextSize(14);
            tvHomeEmergency.setTextSize(14);
            tvHomeNew.setTextColor(Color.parseColor("#333333"));
            tvHomeChronic.setTextColor(Color.parseColor("#3aafff"));
            tvHomeRecover.setTextColor(Color.parseColor("#333333"));
            tvHomeFood.setTextColor(Color.parseColor("#333333"));
            tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
            ivHomeNew.setBackground(null);
            ivHomeChronic.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHomeRecover.setBackground(null);
            ivHomeFood.setBackground(null);
            ivHomeEmergency.setBackground(null);
            initNews(1);
        } else if (position == 2) {
            homeViewPagerNew.setCurrentItem(2);
            tvHomeNew.setTextSize(14);
            tvHomeChronic.setTextSize(14);
            tvHomeRecover.setTextSize(16);
            tvHomeFood.setTextSize(14);
            tvHomeEmergency.setTextSize(14);
            tvHomeNew.setTextColor(Color.parseColor("#333333"));
            tvHomeChronic.setTextColor(Color.parseColor("#333333"));
            tvHomeRecover.setTextColor(Color.parseColor("#3aafff"));
            tvHomeFood.setTextColor(Color.parseColor("#333333"));
            tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
            ivHomeNew.setBackground(null);
            ivHomeChronic.setBackground(null);
            ivHomeRecover.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHomeFood.setBackground(null);
            ivHomeEmergency.setBackground(null);
            initNews(2);
        } else if (position == 3) {
            homeViewPagerNew.setCurrentItem(3);
            tvHomeNew.setTextSize(14);
            tvHomeChronic.setTextSize(14);
            tvHomeRecover.setTextSize(14);
            tvHomeFood.setTextSize(16);
            tvHomeEmergency.setTextSize(14);
            tvHomeNew.setTextColor(Color.parseColor("#333333"));
            tvHomeChronic.setTextColor(Color.parseColor("#333333"));
            tvHomeRecover.setTextColor(Color.parseColor("#333333"));
            tvHomeFood.setTextColor(Color.parseColor("#3aafff"));
            tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
            ivHomeNew.setBackground(null);
            ivHomeChronic.setBackground(null);
            ivHomeRecover.setBackground(null);
            ivHomeFood.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            ivHomeEmergency.setBackground(null);
            initNews(3);
        } else if (position == 4) {
            homeViewPagerNew.setCurrentItem(4);
            tvHomeNew.setTextSize(14);
            tvHomeChronic.setTextSize(14);
            tvHomeRecover.setTextSize(14);
            tvHomeFood.setTextSize(14);
            tvHomeEmergency.setTextSize(16);
            tvHomeNew.setTextColor(Color.parseColor("#333333"));
            tvHomeChronic.setTextColor(Color.parseColor("#333333"));
            tvHomeRecover.setTextColor(Color.parseColor("#333333"));
            tvHomeFood.setTextColor(Color.parseColor("#333333"));
            tvHomeEmergency.setTextColor(Color.parseColor("#3aafff"));
            ivHomeNew.setBackground(null);
            ivHomeChronic.setBackground(null);
            ivHomeRecover.setBackground(null);
            ivHomeFood.setBackground(null);
            ivHomeEmergency.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
            initNews(4);
        }
    }

    private void initNews(final int i) {
        String news_url = Constant.base_url + "/news/list";
        Map<String, String> news_map = new HashMap<>();
        news_map.put("categoryId", i + "");
        news_map.put("page", 1 + "");
        news_map.put("limit", 5 + "");
        Okhttp.postString(false, news_url, news_map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showToast(state);
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 0) {
                        String data = jsonObject.optString("data");
                        JSONObject object1 = new JSONObject(data);
                        String items = object1.optString("items");
                        list_grid.clear();
                        ArrayList<HomeNewsBean> homenewsBeen = JsonUtil.json2beans(items, HomeNewsBean.class);
                        list_grid.addAll(homenewsBeen);
                        HomeNewsAdapter homeNewsAdapter = new HomeNewsAdapter(mContext, list_grid);
                        //homeNewsAdapter.clearDeviceList();
                        homeNewsAdapter.setDeviceList((ArrayList<HomeNewsBean>) list_grid);
                        if (i == 0) {
                            listviewNews.setAdapter(homeNewsAdapter);
                        } else if (i == 1) {
                            listviewChronic.setAdapter(homeNewsAdapter);
                        } else if (i == 2) {
                            listviewRecovery.setAdapter(homeNewsAdapter);
                        } else if (i == 3) {
                            listviewFood.setAdapter(homeNewsAdapter);
                        } else if (i == 4) {
                            listviewEmergency.setAdapter(homeNewsAdapter);
                        }
                        homeNewsAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showToast(state);
            }
        });
    }

    class HomeNewsAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

        private FragmentActivity mContext;
        private List<HomeNewsBean> mList_grid;

        public HomeNewsAdapter(FragmentActivity activity, List<HomeNewsBean> list_grid) {
            mContext = activity;
            mList_grid = list_grid;
        }

        @SuppressWarnings("unchecked")
        public void setDeviceList(ArrayList<HomeNewsBean> list_grid) {
            if (mList_grid != null) {
                mList_grid = (ArrayList<HomeNewsBean>) list_grid.clone();
                notifyDataSetChanged();
            }
        }

        public void clearDeviceList() {
            if (mList_grid != null) {
                mList_grid.clear();
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mList_grid.size() == 0 ? 0 : mList_grid.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.home_item, null);
                viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.home_item_imageview);
                viewHolder.tv_textTitle = (TextView) convertView.findViewById(R.id.home_item_title);
                viewHolder.tv_textview = (TextView) convertView.findViewById(R.id.home_item_text);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String description = mList_grid.get(position).getDescription();
            String name = mList_grid.get(position).getName();
            String pic = mList_grid.get(position).getPic();
            home_id = mList_grid.get(position).getId();
            if (!TextUtils.isEmpty(description)) {
                viewHolder.tv_textview.setText(description);
                viewHolder.tv_textview.setText(description);
            }
            if (!TextUtils.isEmpty(name)) {
                viewHolder.tv_textTitle.setText(name);
            }
            if (!TextUtils.isEmpty(pic)) {
                ImageLoader.display(mContext, pic, viewHolder.iv_pic);
            }
            listviewNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id122 = mList_grid.get(position).getId();
                    Intent intent = new Intent(mContext, HomeDetailActivity.class);
                    intent.putExtra("HOME", id122 + "");
                    intent.putExtra("HOME_TAG", "1");
                    startActivity(intent);
                }
            });
            listviewChronic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id133333 = mList_grid.get(position).getId();
                    Intent intent = new Intent(mContext, HomeDetailActivity.class);
                    intent.putExtra("HOME", id133333 + "");
                    intent.putExtra("HOME_TAG", "2");
                    startActivity(intent);
                }
            });
            listviewRecovery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id_position = mList_grid.get(position).getId();
                    Intent intent = new Intent(mContext, HomeDetailActivity.class);
                    intent.putExtra("HOME", id_position + "");
                    intent.putExtra("HOME_TAG", "3");
                    startActivity(intent);
                }
            });
            listviewFood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id_position = mList_grid.get(position).getId();
                    Intent intent = new Intent(mContext, HomeDetailActivity.class);
                    intent.putExtra("HOME", id_position + "");
                    intent.putExtra("HOME_TAG", "4");
                    startActivity(intent);
                }
            });
            listviewEmergency.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int id_position = mList_grid.get(position).getId();
                    Intent intent = new Intent(mContext, HomeDetailActivity.class);
                    intent.putExtra("HOME", id_position + "");
                    intent.putExtra("HOME_TAG", "5");
                    startActivity(intent);
                }
            });
            return convertView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }

        class ViewHolder {
            ImageView iv_pic;
            TextView tv_textTitle;
            TextView tv_textview;
        }
    }

    @OnClick({R.id.iv_home_weizhi, R.id.tv_home_weizhi, R.id.ll_expert_consultation, R.id.ll_smart_guard, R.id.ll_community_activity, R.id.ll_volunteer_team, R.id.tv_home_new, R.id.tv_home_chronic, R.id.tv_home_recover, R.id.tv_home_food, R.id.tv_home_emergency})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_home_weizhi:
            case R.id.tv_home_weizhi:
                startActivity(new Intent(mContext, SelectCityActivity.class));
                break;
            case R.id.ll_expert_consultation://健康管理
                initGetlogin(ConsultationActivity.class);

                break;
            case R.id.ll_smart_guard://智能守护
                startActivity(new Intent(mContext, Smart_guardActivity.class));
                break;
            case R.id.ll_community_activity://测评
                startActivity(new Intent(mContext, HealthAssActivity.class));
                break;
            case R.id.ll_volunteer_team:
                initVolunteer();
                break;
            case R.id.tv_home_new:
                homeViewPagerNew.setCurrentItem(0);
                tvHomeNew.setTextSize(16);
                tvHomeChronic.setTextSize(14);
                tvHomeRecover.setTextSize(14);
                tvHomeFood.setTextSize(14);
                tvHomeEmergency.setTextSize(14);
                tvHomeNew.setTextColor(Color.parseColor("#3aaffc"));
                tvHomeChronic.setTextColor(Color.parseColor("#333333"));
                tvHomeRecover.setTextColor(Color.parseColor("#333333"));
                tvHomeFood.setTextColor(Color.parseColor("#333333"));
                tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
                ivHomeNew.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHomeChronic.setBackground(null);
                ivHomeRecover.setBackground(null);
                ivHomeFood.setBackground(null);
                ivHomeEmergency.setBackground(null);
                break;
            case R.id.tv_home_chronic:
                homeViewPagerNew.setCurrentItem(1);
                tvHomeNew.setTextSize(14);
                tvHomeChronic.setTextSize(16);
                tvHomeRecover.setTextSize(14);
                tvHomeFood.setTextSize(14);
                tvHomeEmergency.setTextSize(14);
                tvHomeNew.setTextColor(Color.parseColor("#333333"));
                tvHomeChronic.setTextColor(Color.parseColor("#3aaffc"));
                tvHomeRecover.setTextColor(Color.parseColor("#333333"));
                tvHomeFood.setTextColor(Color.parseColor("#333333"));
                tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
                ivHomeNew.setBackground(null);
                ivHomeChronic.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHomeRecover.setBackground(null);
                ivHomeFood.setBackground(null);
                ivHomeEmergency.setBackground(null);
                break;
            case R.id.tv_home_recover:
                homeViewPagerNew.setCurrentItem(2);
                tvHomeNew.setTextSize(14);
                tvHomeChronic.setTextSize(14);
                tvHomeRecover.setTextSize(16);
                tvHomeFood.setTextSize(14);
                tvHomeEmergency.setTextSize(14);
                tvHomeNew.setTextColor(Color.parseColor("#333333"));
                tvHomeChronic.setTextColor(Color.parseColor("#333333"));
                tvHomeRecover.setTextColor(Color.parseColor("#3aaffc"));
                tvHomeFood.setTextColor(Color.parseColor("#333333"));
                tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
                ivHomeNew.setBackground(null);
                ivHomeChronic.setBackground(null);
                ivHomeRecover.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHomeFood.setBackground(null);
                ivHomeEmergency.setBackground(null);
                break;
            case R.id.tv_home_food:
                homeViewPagerNew.setCurrentItem(3);
                tvHomeNew.setTextSize(14);
                tvHomeChronic.setTextSize(14);
                tvHomeRecover.setTextSize(14);
                tvHomeFood.setTextSize(16);
                tvHomeEmergency.setTextSize(14);
                tvHomeNew.setTextColor(Color.parseColor("#333333"));
                tvHomeChronic.setTextColor(Color.parseColor("#333333"));
                tvHomeRecover.setTextColor(Color.parseColor("#333333"));
                tvHomeFood.setTextColor(Color.parseColor("#3aaffc"));
                tvHomeEmergency.setTextColor(Color.parseColor("#333333"));
                ivHomeNew.setBackground(null);
                ivHomeChronic.setBackground(null);
                ivHomeRecover.setBackground(null);
                ivHomeFood.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                ivHomeEmergency.setBackground(null);
                break;
            case R.id.tv_home_emergency:
                homeViewPagerNew.setCurrentItem(4);
                tvHomeNew.setTextSize(14);
                tvHomeChronic.setTextSize(14);
                tvHomeRecover.setTextSize(14);
                tvHomeFood.setTextSize(14);
                tvHomeEmergency.setTextSize(16);
                tvHomeNew.setTextColor(Color.parseColor("#333333"));
                tvHomeChronic.setTextColor(Color.parseColor("#333333"));
                tvHomeRecover.setTextColor(Color.parseColor("#333333"));
                tvHomeFood.setTextColor(Color.parseColor("#333333"));
                tvHomeEmergency.setTextColor(Color.parseColor("#3aaffc"));
                ivHomeNew.setBackground(null);
                ivHomeChronic.setBackground(null);
                ivHomeRecover.setBackground(null);
                ivHomeFood.setBackground(null);
                ivHomeEmergency.setBackgroundColor(getResources().getColor(R.color.titleColorSelected));
                break;
        }
    }

    private void initVolunteer() {
        String voluntee_list_url = Constant.base_url + "/commnet/list.do";
        Map<String, String> volunteerMap = new HashMap<>();

        Okhttp.postString(true, voluntee_list_url, volunteerMap, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {

            }
            @Override
            public void onResponse(String response, int id) {
                //  Log.e("TTTTonLinelist", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status1 = jsonObject.optString("status");
                    int status = Integer.parseInt(status1);
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(mContext, LoginActivity.class));
                        return;
                    }
                    if (status == 43) {
                        startActivity(new Intent(mContext, VolunteerActivity.class));
                    } else {
                        try {
                            JSONObject json = new JSONObject(response);
                            String data = json.optString("data");
                            JSONObject js = new JSONObject(data);
                            String topic = js.optString("topic");
                            JSONObject topJson = new JSONObject(topic);

                            String topicId = topJson.optString("id");
                            String content = topJson.optString("content");
                            String imageUrl = topJson.optString("imageUrl");
                            Intent intent = new Intent(mContext, OnlineVolunteerActivity.class);
                            intent.putExtra("topicId", topicId);
                            intent.putExtra("imageUrl", imageUrl);
                            intent.putExtra("content", content);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }
    private void initGetlogin(final Class<ConsultationActivity> consultationActivityClass) {
        dialog.show();
        Map<String, String> map = new HashMap<>();
        Okhttp.postString(true, url3, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                dialog.cancel();
                Toast.makeText(mContext, state, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.cancel();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16 || status == 34) {
                        startActivity(new Intent(mContext, LoginActivity.class));
                        return;
                    }
                    startActivity(new Intent(mContext, consultationActivityClass));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                dialog.cancel();
                Toast.makeText(mContext, state, Toast.LENGTH_SHORT).show();
            }
        });
    }
}